package com.sailorj.itools.action;

import com.intellij.openapi.actionSystem.AnAction;
import com.intellij.openapi.actionSystem.AnActionEvent;
import com.intellij.openapi.actionSystem.PlatformDataKeys;
import com.intellij.openapi.editor.Editor;
import com.intellij.openapi.module.Module;
import com.intellij.openapi.module.ModuleManager;
import com.intellij.openapi.project.Project;
import com.sailorj.itools.generate.DDLParser;
import com.sailorj.itools.utils.Logger;

import java.io.File;
import java.nio.file.Path;
import java.nio.file.Paths;

public class ParserSqlAction extends AnAction {

    @Override
    public void actionPerformed(AnActionEvent event) {
        //获取编辑器
        Editor mEditor = event.getData(PlatformDataKeys.EDITOR);
        if (mEditor == null) {
            Logger.info("编辑器获取失败");
            return;
        }
        Project project = event.getProject();

        String sql = "CREATE TABLE wx_games_log\n" +
                "(\n" +
                "    id          BIGINT(20)  NOT NULL AUTO_INCREMENT COMMENT '主键',\n" +
                "    ac_id       CHAR(17)    NOT NULL DEFAULT '' COMMENT '活动ID,AC+yyyymmdd+店别+3位自增',\n" +
                "    user_id     CHAR(10)    NOT NULL DEFAULT '' COMMENT '用户ID,WX+8位自增',\n" +
                "    request_id  CHAR(32)    NOT NULL DEFAULT '' COMMENT '游戏场次ID',\n" +
                "    type        TINYINT(2)  NOT NULL DEFAULT 0 COMMENT '操作类型:0.初始化游戏,1.滑动,2.重新开始,3.使用消除卡,4.观看视频复活,5.游戏结束',\n" +
                "    log         JSON                 DEFAULT NULL COMMENT '详情',\n" +
                "    create_id   VARCHAR(50) NOT NULL DEFAULT '' COMMENT '创建人',\n" +
                "    create_time TIMESTAMP   NOT NULL DEFAULT current_timestamp COMMENT '创建时间',\n" +
                "    update_id   VARCHAR(50) NOT NULL DEFAULT '' COMMENT '更新人',\n" +
                "    update_time TIMESTAMP   NOT NULL DEFAULT current_timestamp ON UPDATE current_timestamp COMMENT '更新时间',\n" +
                "    PRIMARY KEY (id)\n" +
                ") ENGINE = InnoDB\n" +
                "  DEFAULT CHARSET = utf8 COMMENT '用户小程序操作日志表';";

        if (project != null) {
            Module[] modules = ModuleManager.getInstance(project).getModules();
            // 执行相关操作
            Module firstModule = modules[0];
            String firstModuleFilePath = firstModule.getModuleFilePath();
            Path firstModulePath = Paths.get(firstModuleFilePath);
            Path firstModuleRootPath = firstModulePath.getParent();
            String mainDirectoryPath = firstModuleRootPath + File.separator + "src" + File.separator + "main" + File.separator + "java" + File.separator;
            System.out.println("当前路径：" + mainDirectoryPath);
            DDLParser.parseSql(sql);
        }
    }

}
